package com.sanyou.controller;

import com.sanyou.pojo.DataInfo;
import com.sanyou.pojo.DataInfoDetail;
import com.sanyou.pojo.vo.SimpleDataInfoVo;
import com.sanyou.service.DataInfoService;
import com.sanyou.utils.JSONResult;
import com.sanyou.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sanyou.controller.BasicController.PAGE_SIZE;

@Api(value = "数据字典相关的接口")
@RestController
@CrossOrigin
@RequestMapping("/dataInfo")
public class DataInfoController {

    @Autowired
    DataInfoService dataInfoService;
    /**
     * 分页获取数据字典记录
     * @param page
     * @param pageSize
     * 请求demo:http://localhost:8081/dataInfo/getDataInfos?page=1&pageSize=10
     * @return
     */
    @ApiOperation(value = "获取数据字典记录", notes = "获取所有数据字典信息记录")
    @PostMapping("/getDataInfos")
    public JSONResult getDataInfos(Integer page, Integer pageSize){

        if(page == null)
            page = 1;

        if(pageSize == null)
            pageSize = PAGE_SIZE;
        PagedResult result = dataInfoService.getAllDataInfo(page,pageSize);
        return JSONResult.ok(result);
    }
    /**
     * 获取所有数据字典记录
     * 请求demo:http://localhost:8081/dataInfo/getAllDataInfos
     * @return
     */
    @ApiOperation(value = "获取所有数据字典信息", notes = "获取所有数据字典信息")
    @PostMapping("/getAllDataInfos")
    public JSONResult getAllDataInfos(){
        return JSONResult.ok(dataInfoService.getAllDataInfo());
    }

    /**
     * 根据目标数据字典的id获取目标数据字典记录下的状态内容的所有字段
     * @param id
     * 请求demo:http://localhost:8081/dataInfo/getDataInfoDetailsById?id=1
     * @return
     */
    @ApiOperation(value = "获取目标数据字典下的所有数据信息", notes = "获取目标数据字典下的所有数据信息")
    @PostMapping("/getDataInfoDetailsById")
    public JSONResult getDataInfoDetailsById(String id){
        if(StringUtils.isBlank(id))
            return JSONResult.errorMsg("数据字典id不能为空!");
        List<DataInfoDetail> list = dataInfoService.getDataInfoDetailsById(id);
        return JSONResult.ok(list);
    }
    /**
     * 根据目表数据字典的字典类型编码获取目标数据字典记录下的状态内容的部分字段
     * @param dataCode
     * 请求demo:http://localhost:8081/dataInfo/getDataInfoDetailsById?id=1
     * @return
     */
    @ApiOperation(value = "根据字典的dataCode获取目标数据字典下的所有已启用数据信息的简易版", notes = "根据字典的dataCode获取目标数据字典下的所有已启用数据信息的简易版")
    @PostMapping("/getSimpleDataInfoDetails")
    public JSONResult getSimpleDataInfoDetails(String dataCode){
        if(StringUtils.isBlank(dataCode))
            return JSONResult.errorMsg("dataCode不能为空!");
        List<SimpleDataInfoVo> list = dataInfoService.getSimpleDataInfoDetailsByDataCode(dataCode);
        return JSONResult.ok(list);
    }
    /**
     * 新增数据字典，必填字段：字典编码，字典名称
     * @param dataInfo
     * @return
     */
    @ApiOperation(value = "新增数据字典", notes = "新增数据字典")
    @PostMapping("/insertDataInfo")
    public JSONResult insertDataInfo(@RequestBody DataInfo dataInfo){
        //数据合法性验证
        if(dataInfo==null
                || StringUtils.isBlank(dataInfo.getDataCode())
                ||StringUtils.isBlank(dataInfo.getDataName())){
            return JSONResult.errorMsg("字典编号或字典名称不允许为空!");
        }

        dataInfoService.upsertDataInfo(dataInfo);
        return JSONResult.ok("数据字典新增成功！");
    }
    /**
     * 新增目标数据字典下的状态，必填字段：字典编码，字典名称，同时检查是否有ParentCode
     * @param dataInfoDetail
     * @return
     */
    @ApiOperation(value = "新增数据字典字段映射", notes = "新增数据字典字段映射")
    @PostMapping("/insertDataInfoDetail")
    public JSONResult insertDataInfoDetail(@RequestBody DataInfoDetail dataInfoDetail){
        //数据合法性验证
        if(dataInfoDetail==null
                || StringUtils.isBlank(dataInfoDetail.getDataCode())
                ||StringUtils.isBlank(dataInfoDetail.getDataName())){
            return JSONResult.errorMsg("字典编号或字典名称不允许为空!");
        }
        if(StringUtils.isBlank(dataInfoDetail.getParentCode())){
            return JSONResult.errorMsg("所属字典编号不能为空!");
        }

        dataInfoService.upsertDataInfoDetail(dataInfoDetail);
        return JSONResult.ok("数据字典字段映射新增成功！");
    }
    /**
     * 更新目标数据字典内容，必填字段：字典编码，字典名称
     * @param dataInfo
     * @return
     */
    @ApiOperation(value = "更新数据字典", notes = "更新数据字典")
    @PostMapping("/updateDataInfo")
    public JSONResult updateDataInfo(@RequestBody DataInfo dataInfo){
        //数据合法性验证
        if(dataInfo==null
                || StringUtils.isBlank(dataInfo.getDataCode())
                ||StringUtils.isBlank(dataInfo.getDataName())){
            return JSONResult.errorMsg("字典编号或字典名称不允许为空!");
        }

        try {
            dataInfoService.upsertDataInfo(dataInfo);
        }catch (Exception e){
            return JSONResult.errorMsg("数据字典更新失败："+e.getMessage());
        }
        return JSONResult.ok("数据字典更新成功！");
    }
    /**
     * 更新目标数据字典下的状态内容，必填字段：字典编码，字典名称
     * @param dataInfoDetail
     * @return
     */
    @ApiOperation(value = "更新数据字典字段映射", notes = "更新数据字典字段映射")
    @PostMapping("/updateDataInfoDetail")
    public JSONResult updateDataInfoDetail(@RequestBody DataInfoDetail dataInfoDetail){
        //数据合法性验证
        if(dataInfoDetail==null
                || StringUtils.isBlank(dataInfoDetail.getDataCode())
                ||StringUtils.isBlank(dataInfoDetail.getDataName())){
            return JSONResult.errorMsg("字典编号或字典名称不允许为空!");
        }
        if(StringUtils.isBlank(dataInfoDetail.getParentCode())){
            return JSONResult.errorMsg("所属字典编号不能为空!");
        }

        //更新数据字典映射
        try {
            dataInfoService.upsertDataInfoDetail(dataInfoDetail);
        }catch (Exception e){
            return JSONResult.errorMsg("数据字典更新失败："+e.getMessage());
        }

        return JSONResult.ok("数据字典字段映射更新成功！");
    }

    @ApiOperation(value = "停用数据字典字段映射", notes = "停用数据字典字段映射")
    @PostMapping("/disableDataInfoDetail")
    public JSONResult disableDataInfoDetail(String id){
        //数据合法性验证
        if(StringUtils.isBlank(id)){
            return JSONResult.errorMsg("字段映射id不允许为空!");
        }
        try {
            dataInfoService.disableDataInfoDetailById(id);
        }catch (Exception e){
            return JSONResult.errorMsg("数据字典停用失败："+e.getMessage());
        }
        return JSONResult.ok("数据字典字段映射停用成功！");
    }

    @ApiOperation(value = "启用数据字典字段映射", notes = "启用数据字典字段映射")
    @PostMapping("/enableDataInfoDetail")
    public JSONResult enableDataInfoDetail(String id){
        //数据合法性验证
        if(StringUtils.isBlank(id)){
            return JSONResult.errorMsg("字段映射id不允许为空!");
        }
        try {
            dataInfoService.enableDataInfoDetailById(id);
        }catch (Exception e){
            return JSONResult.errorMsg("数据字典启用失败："+e.getMessage());
        }
        return JSONResult.ok("数据字典字段映射启用成功！");
    }

    @ApiOperation(value = "删除数据字典", notes = "删除数据字典")
    @PostMapping("/deleteDataInfo")
    public JSONResult deleteDataInfo(String id){
        //数据合法性验证
        if(StringUtils.isBlank(id)){
            return JSONResult.errorMsg("字典id不允许为空!");
        }

        try {
            dataInfoService.deleteDataInfoById(id);
        }catch (Exception e){
            return JSONResult.errorMsg("数据字典删除失败："+e.getMessage());
        }
        return JSONResult.ok("数据字典删除成功！");
    }

    @ApiOperation(value = "根据id删除数据字典字段映射", notes = "根据id删除数据字典字段映射")
    @PostMapping("/deleteDataInfoDetail")
    public JSONResult deleteDataInfoDetail(String id){
        //数据合法性验证
        if(StringUtils.isBlank(id)){
            return JSONResult.errorMsg("字段映射id不允许为空!");
        }
        try {
            dataInfoService.deleteDataInfoDetailById(id);
        }catch (Exception e){
            return JSONResult.errorMsg("数据字典删除失败："+e.getMessage());
        }
        return JSONResult.ok("数据字典字段映射删除成功！");
    }

    @ApiOperation(value = "根据id获取字典字段映射", notes = "根据id获取字典字段映射")
    @PostMapping("/getDataInfoDetail")
    public JSONResult getDataInfoDetail(String id){
        //数据合法性验证
        if(StringUtils.isBlank(id)){
            return JSONResult.errorMsg("字段映射id不允许为空!");
        }
        return JSONResult.ok(dataInfoService.getDataInfoDetailById(id));
    }

    @ApiOperation(value = "根据id获取字典", notes = "根据id获取字典")
    @PostMapping("/getDataInfo")
    public JSONResult getDataInfo(String id){
        //数据合法性验证
        if(StringUtils.isBlank(id)){
            return JSONResult.errorMsg("字段映射id不允许为空!");
        }
        return JSONResult.ok(dataInfoService.getDataInfoById(id));
    }
}
